<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Pc_Api_Standard_Coupon extends Controller_Pc_Api_Base
{
    public function before()
    {
        parent::before();
    }

    //优惠券列表
    public function action_receive_list()
    {
        $page = intval($this->request_body->content->page) or $page = 1;
        $pagesize = intval($this->request_body->content->pagesize) or $pagesize = 10;
        Common::session('member', array('mid' => $this->request_body->content->memberid));
        $route_array = array(
            'controller' => $this->request->controller(),
            'action' => $this->request->action(),
        );
        $out = Model_Coupon::search_result($route_array, $page, $pagesize, 0, 0);
        $result = Model_Coupon::get_data($out['list']);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    //获取优惠券
    public function action_receive()
    {
        $memberid = intval($this->request_body->content->memberid);
        $id = intval($this->request_body->content->id);
        Common::session('member', array('mid' => $memberid));
        //根据优惠券扣积分
        $coupon = DB::select()->from('coupon')->where('id','=',$id)->execute()->current();
        $status = Model_Coupon::xcx_check_receive_status($id,$memberid);
        //判断发卷方式 1 免费  2 积分
        if($coupon['kindid'] == '2')
        {
            //可以领取
            if($status['status'] == '1')
            {
                //需要积分
                $needjifen = $coupon['needjifen'];
                //判断用户积分是否满足领取积分的条件
                $member_jifen = DB::select('jifen')->from('member')->where('mid','=',$memberid)->execute()->get('jifen');
                if($member_jifen >= $needjifen)
                {
                    //领取优惠券
                    Model_Coupon::get_coupon($id, $memberid);
                    //扣除积分并写记录
                    $jifen = array(
                        'jifen'=>$member_jifen-$needjifen
                    );
                    DB::update('member')->set($jifen)->where('mid','=',$memberid)->execute();
                    $jifen_log = array(
                        'memberid' => $memberid,
                        'content' => '积分领取优惠券:'.$coupon['name'] .',使用'.$needjifen.'积分',
                        'addtime' => time(),
                        'type' => 1,
                        'jifen' => $needjifen,
                    );
                    $query = DB::insert('member_jifen_log', array_keys($jifen_log))->values(array_values($jifen_log))->execute();
                    if($query)
                    {
                        $result = array('status' => 1, 'receive' => 1);
                    }
                }
                else
                {
                    $result = array('status' => false, 'receive' => 0,'msg'=> '积分不足');
                }
            }
            else
            {
                $result = array('status' => $status['status'], 'receive' => 1);
            }
        }
        else
        {
            //免费领取 可以领取
            if($status['status'] == '1')
            {
                Model_Coupon::get_coupon($id, $memberid);
            }
            $result = array('status' => $status['status'], 'receive' => 1);
        }

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    //会员优惠券
    public function action_own()
    {
        $page = intval($this->request_body->content->page) or $page = 1;
        $pagesize = intval($this->request_body->content->pagesize) or $pagesize = 10;
        Common::session('member', array('mid' => $this->request_body->content->memberid));
        $result = array();
        //未使用
        $params = array('isout' => 1, 'kindid' => 0);
        $out = Model_Coupon::member_search_result($params, $page, $pagesize);
        $result['use'] = Model_Coupon::get_data($out['list']);
        //已使用
        $params = array('isout' => 3, 'kindid' => 0);
        $out = Model_Coupon::member_search_result($params, $page, $pagesize);
        $result['used'] = Model_Coupon::get_data($out['list']);
        //已失效
        $params = array('isout' => 2, 'kindid' => 0);
        $out = Model_Coupon::member_search_result($params, $page, $pagesize);
        $result['nouse'] = Model_Coupon::get_data($out['list']);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    //可用优惠券
    public function action_use_list()
    {
        $type_id = Common::remove_xss($this->request_body->content->model_type_id);
        $product_id = Common::remove_xss($this->request_body->content->product_id);
        Common::session('member', array('mid' => $this->request_body->content->memberid));
        $result = Model_Coupon::get_pro_coupon($type_id, $product_id);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    public function action_couponSearch()
    {
        $cid = Common::remove_xss($this->request_body->content->cid);
        $typeid = Common::remove_xss($this->request_body->content->typeid);
        $keyword = Common::remove_xss($this->request_body->content->keyword);
        $page = intval($this->request_body->content->page);
        $result = Model_Coupon::get_couponSearch($cid,$typeid,$keyword,$page);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    // 获取产品可用优惠券
    public function action_get_coupon_list()
    {

        $type_id = intval($this->request_body->content->typeid);
        $product_id = intval($this->request_body->content->productid);
        $mid = intval($this->request_body->content->mid);

        $coupon_list = array();
        // 更加ID查询可用的优惠券
        if(St_Functions::is_normal_app_install('coupon') && !empty($mid))
        {
            $coupon_list = Model_Coupon::get_product_coupon_list($product_id,$type_id);
            // 处理个人优惠券
            foreach ($coupon_list as &$v)
            {
                $receive_status = Model_Api_Standard_Coupon::check_receive_status($v['id'],$mid,$v['totalnumber'],$v['maxnumber']);
                $v['receive_status'] = $receive_status;
            }
        }

        $result = array('status'=> true, "list"=> $coupon_list);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }
}